package com.powernode.model;

import com.powernode.constant.ResponseCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @author syo 2319615164@qq.com
 * @description 统一响应结果对象
 * @date 2024/10/1
 * @Copyright syo
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@ApiModel("统一响应结果对象")
public class Result<T> implements Serializable {

    @ApiModelProperty("状态码")
    private Integer code = 200;

    @ApiModelProperty("消息")
    private String msg = "ok";

    @ApiModelProperty("数据")
    private T data;

    /**
     * 成功响应
     * @param data 数据
     * @return 响应结果
     * @param <T> 泛型
     */
    public static <T> Result<T> success(T data) {
        return new Result<T>(200, "ok", data);
    }

    /**
     * 失败响应
     * @param code 状态码
     * @param msg  响应消息
     * @param data 响应数据
     * @return  返回结果
     * @param <T> 泛型
     */
    public static <T> Result<T> fail(Integer code, String msg, T data) {
        Result<T> result = new Result<T>();
        result.setCode(code);
        result.setMsg(msg);
        result.setData(data);
        return result;
    }

    /**
     * 失败响应对象
     * @param responseCode 失败枚举
     * @param data 数据
     * @return 结果
     * @param <T> 泛型
     */
    public static <T> Result<T> fail(ResponseCode responseCode, T data) {
        Result<T> result = new Result<T>();
        result.setCode(responseCode.getCode());
        result.setMsg(responseCode.getDesc());
        result.setData(data);
        return result;
    }
}
